package cn.good.biz.service;

import cn.good.pojo.Answer;
import cn.good.pojo.Issue;
import cn.good.service.AnswerService;
import cn.good.service.IssueService;
import cn.good.utils.EmptyUtils;
import cn.good.vo.IssueInfoVO;
import cn.good.vo.IssueListVO;
import cn.good.vo.IssueSaveVO;
import cn.good.vo.IssueSearchVO;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class GDIssueServiceImpl implements GDIssueService {

    @Reference
    private IssueService issueService;

    @Reference
    private AnswerService answerService;

    @Override
    public boolean saveIssue(IssueSaveVO issueSaveVO)throws Exception {
        Issue issue = new Issue();
        BeanUtils.copyProperties(issueSaveVO,issue);
        issue.setCreatedBy(issue.getId());
        issue.setCreationDate(new Date());
        return issueService.saveIssue(issue)>0;
    }

    @Override
    public IssueListVO searchIssueList(IssueSearchVO issueSearchVO, Integer pageNo) throws Exception {

        Issue issue = new Issue();
        if(pageNo==null|| pageNo<1)
            pageNo=1;
        if(!EmptyUtils.isEmpty(issueSearchVO)){
            if(EmptyUtils.isEmpty(issueSearchVO.getUserId())){
             issue.setUserId(issueSearchVO.getUserId());
            }
        }


        return issueService.selectListByPage(issue,pageNo);
    }

    /**
     * 根据id查询问题
     * @param id
     */
    @Override
    public IssueInfoVO searchIssueInfo(Integer id) throws Exception{

        IssueInfoVO issueInfoVO = new IssueInfoVO();

        Issue issue = issueService.selectBuId(id);

        BeanUtils.copyProperties(issue,issueInfoVO);

        List<Answer> answerList = answerService.serchListById(Long.parseLong(id.toString()));

        issueInfoVO.setData(answerList);

        return issueInfoVO;
    }
}
